package org.hk.kg.finance.costype.action;

import java.util.Map;
import java.util.Map.Entry;

import org.hk.kg.db.DBServiceProxy;
import org.hk.kg.finance.FinanceConstants;
import org.hk.kg.finance.costype.service.CostypeService;
import org.hk.kg.finance.costype.service.ICostypeService;
import org.hk.kg.ui.KGUiContext;
import org.hk.kg.ui.ParamsStack;
import org.hk.kg.utils.Constants;
import org.hk.kg.utils.StringUtils;
import org.hk.kg.web.BaseAction;

public class NewCostypeAction extends BaseAction {
	private ICostypeService costypeService = DBServiceProxy.getProxyClz(CostypeService.class);
	
	public String doCreate(ParamsStack params)
	{
		String costypeName = params.getValue("costypeName"),
			costypeKindStr = params.getValue("costypeKind"),
			activeStr = params.getValue("active");
		if(StringUtils.isEmpty(costypeKindStr))
		{
			KGUiContext.instance.error(Constants.COMMON_FAIL_WINDOW_TITLE, 
					FinanceConstants.ERROR_NONE_COSTYPEKIND);
			return None;
		}
		int costTypeKind = Integer.parseInt(costypeKindStr);
		boolean active = false;
		if("true".equals(activeStr))
		{
			active = true;
		}
		if(costypeService.createCostType(costypeName, costTypeKind, active))
		{
			return "cost_type";
		}
		else
		{
			return Constants.COMMON_FAIL_ACTION;
		}
	}
	
	public String getCostypeKinds()
	{
		Map<String, String> clzTypes = Constants.CONFIG.getGroup(FinanceConstants.COST_TYPE_KINDS);
		StringBuffer buffer = new StringBuffer();
		for(Entry<String, String> entry : clzTypes.entrySet())
		{
			buffer.append("<option value=\"").append(entry.getKey()).append("\">")
			.append(entry.getValue()).append("</option>\n");
		}
		return buffer.toString();
	}
}
